1
|
|
|
import axios from "axios" |
2
|
|
|
import { graphql, useStaticQuery } from "gatsby" |
3
|
|
|
import React, { Fragment, FunctionComponent, useEffect, useState } from "react" |
4
|
|
|
|
5
|
|
|
import { sortRankings } from "../scripts/helper" |
6
|
|
|
import "./MiniRanking.scss" |
7
|
|
|
import Spinner from "./Spinner" |
8
|
|
|
|
9
|
|
|
const MiniRanking: FunctionComponent<MiniRankingProps> = ({ teamId, homeTeam, awayTeam }: MiniRankingProps) => { |
10
|
|
|
useEffect(() => { |
11
|
|
|
async function getData() { |
12
|
|
|
const response = await axios.get(`${kcvvPsdApi}/ranking/${teamId}`) |
13
|
|
|
setData(response.data) |
14
|
|
|
} |
15
|
|
|
getData() |
16
|
|
|
}, [kcvvPsdApi, teamId]) |
17
|
|
|
|
18
|
|
|
return ( |
19
|
|
|
<section className={`ranking__wrapper`}> |
20
|
|
|
{data.length > 0 || <Spinner />} |
21
|
|
|
{data && renderRankings(data, homeTeam, awayTeam)} |
22
|
|
|
</section> |
23
|
|
|
) |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
const renderRanking = (ranking: RankingDataObject, homeTeam: string, awayTeam: string, i: number): JSX.Element => ( |
27
|
|
|
<div className={`ranking`} key={i}> |
28
|
|
|
<h4>{ranking.name.replace(`Voetbal : `, ``)}</h4> |
29
|
|
|
<table> |
30
|
|
|
<thead> |
31
|
|
|
<tr> |
32
|
|
|
<th className={`table__column__number`}>#</th> |
33
|
|
|
<th className={`table__column__string`}>Team</th> |
34
|
|
|
<th className={`table__column__number show-for-medium`}>P</th> |
35
|
|
|
<th className={`table__column__number`}>W</th> |
36
|
|
|
<th className={`table__column__number`}>D</th> |
37
|
|
|
<th className={`table__column__number`}>L</th> |
38
|
|
|
<th className={`table__column__number show-for-medium`}>G+</th> |
39
|
|
|
<th className={`table__column__number show-for-medium`}>G-</th> |
40
|
|
|
<th className={`table__column__number`}>+/-</th> |
41
|
|
|
<th className={`table__column__number`}>Pts</th> |
42
|
|
|
</tr> |
43
|
|
|
</thead> |
44
|
|
|
<tbody> |
45
|
|
|
{ranking.teams.sort(sortRankings).map( |
46
|
|
|
(team: RankingDataTeamObject, j: number) => |
47
|
|
|
(team.team?.club?.localName.includes(homeTeam) || team.team?.club?.localName.includes(awayTeam)) && ( |
48
|
|
|
<tr key={j}> |
49
|
|
|
<td className={`table__column__number`}>{team.rank || `-`}</td> |
50
|
|
|
<td |
51
|
|
|
className={`table__column__string ${ |
52
|
|
|
team.team?.club?.localName?.toLowerCase().includes(`elewijt`) ? `table__column--highlight` : `` |
53
|
|
|
}`} |
54
|
|
|
> |
55
|
|
|
{team.team?.club?.localName || ``} |
56
|
|
|
</td> |
57
|
|
|
<td className={`table__column__number show-for-medium`}>{team.matchesPlayed || `0`}</td> |
58
|
|
|
<td className={`table__column__number`}>{team.wins || `0`}</td> |
59
|
|
|
<td className={`table__column__number`}>{team.draws || `0`}</td> |
60
|
|
|
<td className={`table__column__number`}>{team.losses || `0`}</td> |
61
|
|
|
<td className={`table__column__number show-for-medium`}>{team.goalsScored || `0`}</td> |
62
|
|
|
<td className={`table__column__number show-for-medium`}>{team.goalsConceded || `0`}</td> |
63
|
|
|
<td className={`table__column__number`}>{team.goalsScored - team.goalsConceded || `0`}</td> |
64
|
|
|
<td className={`table__column__number`}>{team.points || `0`}</td> |
65
|
|
|
</tr> |
66
|
|
|
) |
67
|
|
|
)} |
68
|
|
|
</tbody> |
69
|
|
|
</table> |
70
|
|
|
</div> |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
const renderRankings = (rankings: RankingDataObject[], homeTeam: string, awayTeam: string): JSX.Element => ( |
74
|
|
|
<Fragment> |
75
|
|
|
{rankings |
76
|
|
|
.filter((ranking: RankingDataObject) => ranking.teams.length > 0) |
77
|
|
|
.sort((a, b) => a.name.localeCompare(b.name)) |
78
|
|
|
.map((ranking: RankingDataObject, i: number) => i === 0 && renderRanking(ranking, homeTeam, awayTeam, i))} |
79
|
|
|
</Fragment> |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
export default MiniRanking |
83
|
|
|
|